﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AllDemo
{
    public partial class 判断一点是否在圆上 : Form
    {
        public 判断一点是否在圆上()
        {
            InitializeComponent();
            MouseDown += new MouseEventHandler(OnMouseDown);
            centerOfCircle.X = 150;
            centerOfCircle.Y = 150;
            this.DoubleBuffered = true;    //开启二次缓存


        }
        private Point centerOfCircle = new Point();   //圆心
        private Point clickPoint = new Point();
        private int r = 90;                           //半径
        Graphics g;
        private void drawCircle()
        {
            g.SmoothingMode = SmoothingMode.HighQuality;  //图片柔顺模式选择
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;//高质量
            g.CompositingQuality = CompositingQuality.HighQuality;//再加一点
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.FillEllipse(new SolidBrush(Color.Green), centerOfCircle.X-r, centerOfCircle.Y-r, 2*r, 2*r);
            g.DrawRectangle(Pens.Red, centerOfCircle.X - r-1, centerOfCircle.Y - r-1, 2 * r+2, 2 * r+2);
            g.DrawLine(Pens.Silver, (float)centerOfCircle.X-5, centerOfCircle.Y, centerOfCircle.X+5, centerOfCircle.Y);
            g.DrawLine(Pens.Silver, (float)centerOfCircle.X, centerOfCircle.Y-5, centerOfCircle.X, centerOfCircle.Y+5);
            g.DrawLine(Pens.Orange, clickPoint.X - 5, clickPoint.Y, clickPoint.X + 5, clickPoint.Y);
            g.DrawLine(Pens.Orange, clickPoint.X , clickPoint.Y-5, clickPoint.X , clickPoint.Y+5);
            Pen pen = new Pen(Color.Gold, 2);

        }
        void OnMouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {

                clickPoint.X = e.X;
                clickPoint.Y = e.Y;
                txt_x.Text = e.X.ToString();
                txt_y.Text = e.Y.ToString();
                checkPointIsInCircle(e.Location);
                Invalidate();
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            g = e.Graphics;
            drawCircle();
        }



        //判断一点是否在圆上，依据点与圆的位置关系，（x-a)^2+(y-b)^2=r 则该点在圆上,>r则在圆外，<r则在圆内
        private void checkPointIsInCircle(Point _clickPoint)
        {
            int tempR = (_clickPoint.X - centerOfCircle.X) *(_clickPoint.X - centerOfCircle.X) + (_clickPoint.Y - centerOfCircle.Y)* (_clickPoint.Y - centerOfCircle.Y);
            
            if (tempR > r*2)
            {
                txt_show.Text="该点在圆外";
                
            }

            if (Math.Abs(tempR- r * r)<30 )
            {
                txt_show.Text = "该点在圆上";
            }

            if (tempR < r * r)
            {
                txt_show.Text = "该点在圆内";
            }
        }
    }
}
